﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using AjaxControlToolkit;

public partial class Controls_ListPopup : System.Web.UI.UserControl, ITextControlExtension, IListControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        PopupControlExtender.GetProxyForCurrentPopup(Page).Commit(string.Empty);
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (SelectedIndexChanged == null)
        {
            PopupControlExtender.GetProxyForCurrentPopup(Page).Commit(txtOther.Text);
        }
        else
        {
            Text = txtOther.Text;
            SelectedIndexChanged(this, EventArgs.Empty);
        }                
    }

    protected void rp_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Show":
                Text = e.CommandArgument as string;

                if (SelectedIndexChanged == null)
                {                    
                    PopupControlExtender.GetProxyForCurrentPopup(Page).Commit((e.CommandSource as LinkButton).Text);
                }
                else
                {                    
                    SelectedIndexChanged(this, EventArgs.Empty);
                }
                
                break;
        }
    }

    public object DataSource
    {
        get
        {
            return rp.DataSource;
        }
        set
        {
            rp.DataSource = value;
        }
    }

    public string DataTextField
    {
        get
        {
            return ViewState["DataTextField"] as string;
        }
        set
        {
            ViewState["DataTextField"] = value;
        }
    }

    public string DataValueField
    {
        get
        {
            return ViewState["DataValueField"] as string;
        }
        set
        {
            ViewState["DataValueField"] = value;
        }
    }

    public event EventHandler SelectedIndexChanged;

    #region ITextControlExtension 成員

    string ITextControlExtension.SelectedText
    {
        get
        {
            throw new Exception("The method or operation is not implemented.");
        }
        set
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

    string ITextControlExtension.FindTextByValue(string value)
    {
        if (txtOther.Text != string.Empty)
        {
            return txtOther.Text;
        }

        foreach (ListViewItem item in rp.Items)
        {
            LinkButton lkn = item.FindControl("lkn") as LinkButton;

            if (lkn.CommandArgument == value)
            {
                return lkn.Text;
            }
        }

        return string.Empty;
    }

    string ITextControlExtension.FindValueByText(string text)
    {
        if (txtOther.Text != string.Empty)
        {
            return txtOther.Text;
        }

        foreach (ListViewItem item in rp.Items)
        {
            LinkButton lkn = item.FindControl("lkn") as LinkButton;

            if (lkn.Text == text)
            {
                return lkn.CommandArgument;
            }
        }

        return string.Empty;
    }

    #endregion

    #region ITextControl 成員

    public string Text
    {
        get
        {
            if (txtOther.Text != string.Empty)
            {
                return txtOther.Text;
            }

            return ViewState["Text"] as string;            
        }
        set
        {
            ViewState["Text"] = value;
        }
    }

    #endregion

    public bool Clearable
    {
        get
        {
            return plClear.Visible;
        }
        set
        {
            plClear.Visible = value;
        }
    }

    #region IDataBoundControl 成員

    public bool AllowOther
    {
        get
        {
            return plOther.Visible;
        }
        set
        {
            plOther.Visible = value;
        }
    }

    public event EventHandler DataBound;

    #endregion
}
